package com.jiangchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiangchuang.domain.MinioProperties;
import com.jiangchuang.domain.entity.*;
import com.jiangchuang.domain.response.GetImgInfoRes;
import com.jiangchuang.mapper.*;
import com.jiangchuang.service.IDishService;
import com.jiangchuang.service.IReviewService;
import com.jiangchuang.utils.CommonUtils;
import com.jiangchuang.utils.MinIoUtil;
import com.jiangchuang.utils.OssUtils;
import com.jiangchuang.utils.UserContextUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.RowBounds;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ReviewServiceImpl implements IReviewService {
    @Autowired
    private WxsecurityTraceMapper wxsecurityTraceMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private IDishService dishService;
    @Resource
    private MinioProperties minioProperties;
    @Autowired
    private ViolateUserMapper violateUserMapper;
    @Autowired
    private UserAdminMapper adminMapper;

    @Override
    public GetImgInfoRes getImgInfo(Integer id) {
        WxsecurityTrace trace = wxsecurityTraceMapper.selectById(id);
        GetImgInfoRes res = new GetImgInfoRes();

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("openid",trace.getOpenid());
        List<User> users = userMapper.selectList(userQueryWrapper);
        if(users.size()==1){
            res.setTraceId(id);
            res.setUrl(trace.getImgUrl());
            res.setUid(users.get(0).getId());
            res.setDishList(dishService.getUserDishPage(users.get(0).getId(), 1, 20).getList());
        }else{
            throw new RuntimeException("查询到重复openid:"+ trace.getOpenid());
        }

        return res;
    }

    @Override
    public void reviewImg(Integer id, Integer reviewStatus) {
        if(reviewStatus==4){
            // 审核通过则从待审核表中删除
            wxsecurityTraceMapper.deleteById(id);
        } else if (reviewStatus==3) {
            // 审核不通过
            WxsecurityTrace trace = wxsecurityTraceMapper.selectById(id);
//            String path = trace.getImgUrl().replace("https://sanshipu.jianguijizhang.com/minio/"+minioProperties.getBucketName(),"");
            String path = trace.getImgUrl().replace("https://sanshipu.jianguijizhang.com/oss/fanquan/","");
            // 违规提示图
            InputStream inputStream = null;
            try {
                inputStream = CommonUtils.downloadImg("https://sanshipu.jianguijizhang.com/minio/fanquan/static/imgViolation.png");
            } catch (IOException e) {
                throw new RuntimeException("违规警告图片下载失败"+e);
            }
            OssUtils.upload(path, inputStream);
//            MinIoUtil.upload(minioProperties.getBucketName(), path, inputStream);
            log.info("【图片违规】====用户openid:"+trace.getOpenid());

            QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
            userQueryWrapper.eq("openid",trace.getOpenid());
            List<User> users = userMapper.selectList(userQueryWrapper);
            // 违规信息存入违规用户表
            if(users.size()==1){
                ViolateUser violateUser = new ViolateUser();
                violateUser.setOpenid(trace.getOpenid());
                violateUser.setUid(users.get(0).getId());
                violateUser.setNickname(users.get(0).getNickname());
                violateUserMapper.insert(violateUser);
            }
            wxsecurityTraceMapper.deleteById(id);
        }
    }

    @Override
    public void violateImg(String imgUrl) {
        String path = imgUrl.replace("https://sanshipu.jianguijizhang.com/oss/fanquan/","");
        // 违规提示图
        InputStream inputStream = null;
        try {
            inputStream = CommonUtils.downloadImg("https://sanshipu.jianguijizhang.com/minio/fanquan/static/imgViolation.png");
        } catch (IOException e) {
            throw new RuntimeException("违规警告图片下载失败"+e);
        }
        OssUtils.upload(path, inputStream);

//        MinIoUtil.upload(minioProperties.getBucketName(), path, inputStream);
    }

    @Override
    public void replaceDomian(int idx, int num) {
        List<Dish> dishes = dishMapper.getLimitDish(idx,num);
//        List<WxsecurityTrace> list = wxsecurityTraceMapper.selectList(null);
        for (Dish item : dishes){
            if(item.getCover().contains("https://jiangchuang-fanquan.oss-cn-hangzhou.aliyuncs.com/")){
                String newUrl = item.getCover().replace("https://jiangchuang-fanquan.oss-cn-hangzhou.aliyuncs.com/","https://sanshipu.jianguijizhang.com/oss/fanquan/");
                item.setCover(newUrl);
                dishMapper.updateById(item);
            }
        }

    }
}
